package paytreasure.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import paytreasure.po.Moneyinout;
import paytreasure.service.MoneyinoutService;
import paytreasure.util.ConstantUtil;
import paytreasure.util.ObjAnalysis;
import paytreasure.vo.ResultVo;

/**
 * 
 * @author xhh
 * @date 2017年7月13日
 *  钱来钱往
 */
@Controller
@RequestMapping(value="/moneyinout")
public class MoneyinoutController {

	private static final Logger LOGGER = LoggerFactory.getLogger(MoneyinoutController.class);
	
	@Resource
	private MoneyinoutService moneyinoutService ;
	
	private ResultVo result;


	/**
	 * 
	 * @param 
	 * @return 保存添加钱来钱往信息
	 * @return_type: String
	 *
	 */
    @RequestMapping(value = "/insertMoneyinout.action", method = { RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
	public String insertMoneyinout(@RequestBody Map<String, Object> map) throws Exception {

    	Moneyinout mon = new Moneyinout();
    	try {
//    		BeanUtils.populate(mon, map);
    		ObjAnalysis.transMap2Bean(map, mon);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("保存钱来钱往信息失败！");
		}
		
    	mon.setMoneyId(UUID.randomUUID().toString());
		mon.setCreateTime(new Date());
		result = moneyinoutService.insertMoneyinout(mon);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}

    /**
	 * 
	 * @param 
	 * @return  通过id查询，去钱来钱往信息页面
	 * @return_type: String
	 *
	 */
	@RequestMapping(value="/toMoneyinoutMessage.action", method={RequestMethod.POST},produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String toMoneyinoutMessage(@RequestBody Map<String, String> map) throws Exception {
		result = moneyinoutService.getMoneyinoutById(map.get("moneyId"));
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
	/**
	 * 
	 * @param 
	 * @return  通过id查询，去修改钱来钱往信息页面
	 * @return_type: String
	 *
	 */
	@RequestMapping(value="/toEditMoneyinout.action", method={RequestMethod.POST},produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String toEditMoneyinout(@RequestBody Map<String, String> map) throws Exception {
		ResultVo result = moneyinoutService.getMoneyinoutById(map.get("moneyId"));
		if(result == null){
			return JSONObject.toJSONString(result, ConstantUtil.vFilter);
		}
		result = new ResultVo();
		result.isResultSuccess("success", result);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}

	/**
	 * 
	 * @param id
	 * @return 通过id查询，修改钱来钱往信息，保存修改
	 * @throws Exception
	 */
	@RequestMapping(value="/saveEditMoneyinout.action", method={RequestMethod.POST},produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String saveEditMoneyinout(@RequestBody HashMap<String, Object> map) throws Exception {
		
		Moneyinout mon = new Moneyinout();
		try {
//    		BeanUtils.populate(mon, map);
			ObjAnalysis.transMap2Bean(map, mon);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("保存钱来钱往信息失败！");
		}
		mon.setUpdateTime(new Date());
		moneyinoutService.updateMoneyinout(mon);
		result = new ResultVo();
		result.isResultSuccess();
		result.setResultObj(mon);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
		
	 /**
	  * 
	  * @param 
	  * @return
	  *
	  */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

}